package com.sgb.kjwl.view.ui.adapter.self.publish;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.GoodsListEntity3;
import com.sgb.kjwl.view.ui.activity.self.publish.GoodsPublishActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class GoodsListAdapter3 extends BaseRecycleAdapter<GoodsListEntity3> {

    private TextView statusView;
    private TextView contentView;
    private TextView priceView;
    private TextView timeView;
    private TextView editView;
    private TextView upView;
    private TextView downView;
    private TextView delView;
    private ItemCallBack itemCallBack;

    public GoodsListAdapter3(Context context, List<GoodsListEntity3> datas) {
        super(context, R.layout.item_goods_list3, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final GoodsListEntity3 entity) {
        holder.setBinding(BR.viewModel, entity);
        statusView = holder.getItem().findViewById(R.id.status_view);
        contentView = holder.getItem().findViewById(R.id.content_view);
        priceView = holder.getItem().findViewById(R.id.price_view);
        timeView = holder.getItem().findViewById(R.id.time_view);
        editView = holder.getItem().findViewById(R.id.edit_view);
        upView = holder.getItem().findViewById(R.id.up_view);
        downView = holder.getItem().findViewById(R.id.down_view);
        delView = holder.getItem().findViewById(R.id.del_view);
        String status = entity.getStatus();
        if (status.equals("已下架")) {
            upView.setVisibility(View.VISIBLE);
            downView.setVisibility(View.GONE);
            statusView.setBackgroundResource(R.drawable.corner2_969696_full_bg);
            editView.setBackgroundResource(R.drawable.bg_shap_color_change_yellow);
        } else if (status.equals("已上架")) {
            upView.setVisibility(View.GONE);
            downView.setVisibility(View.VISIBLE);
            statusView.setBackgroundResource(R.drawable.corner2_06c764_full_bg);
            editView.setBackgroundResource(R.drawable.bg_shap_color_change_yellow_40);
        } else {
            upView.setVisibility(View.GONE);
            downView.setVisibility(View.GONE);
            statusView.setBackgroundResource(R.drawable.corner2_c0c4cc_full_bg);
            editView.setBackgroundResource(R.drawable.bg_shap_color_change_yellow);
        }
        if (entity.getCid1().equals("7")) {
            contentView.setText(entity.getStorage());
        } else {
            contentView.setText((TextUtils.isEmpty(entity.getBrandName()) ? "" : entity.getBrandName()+ "   | " )+ entity.getStorage());
        }
        editView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (status.equals("已上架")) {
                    return;
                } else {
                    if (entity.getCid1().equals("7")) {
                        GoodsPublishActivity.start(context, entity.getId(), true);
                    } else {
                        if (entity.isPromote()) {
                            ToastUtil.show(context, "促销商品暂不能操作");
                        } else {
                            GoodsPublishActivity.start(context, entity.getId(), false);
                        }
                    }
                }
            }
        });
        upView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (entity.isPromote()) {
                    ToastUtil.show(context, "促销商品暂不能操作");
                } else {
                    if (itemCallBack != null) {
                        itemCallBack.up(entity.getId());
                    }
                }
            }
        });
        downView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (entity.isPromote()) {
                    ToastUtil.show(context, "促销商品暂不能操作");
                } else {
                    if (itemCallBack != null) {
                        itemCallBack.down(entity.getId());
                    }
                }
            }
        });
        delView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (entity.isPromote()) {
                    ToastUtil.show(context, "促销商品暂不能操作");
                } else {
                    if (itemCallBack != null) {
                        itemCallBack.del(entity.getId());
                    }
                }
            }
        });
        priceView.setText("¥ " + entity.getPriceScope() + "/" + entity.getUnit());
        timeView.setText("更新时间： " + entity.getUpdateTime());
    }

    public interface ItemCallBack {
        void up(String id);

        void down(String id);

        void del(String id);
    }

    public void setItemCallBack(ItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }
}
